﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace GW2_Marketeer
{
	class GuildWarsTradeRequest
	{
		private const string m_sGW2SpidyURL = "http://www.guildwarstrade.com/item/";
		private string m_sItemID;
		static Random ran = new Random();

		public GuildWarsTradeRequest(string sItemID)
		{
			m_sItemID = sItemID;
		}

		public int GetPrice()
		{
			string sWebPageString = GetWebPageAsString();
 			int nPrice = GetPriceFromString(sWebPageString);
			return nPrice;
		}

		public int GetPriceTest()
		{
			int ret = ran.Next(10000);
			return ret;
		}

		public int GetPriceTestString()
		{
			string sWebPageString = GetTestString();
 			int nPrice = GetPriceFromString(sWebPageString);
			return nPrice;			
		}

		private int GetPriceFromString(string sWebPageString)
		{
			// Substring starting at end of"Sell</strong></td><td class="align-right">"
			int nIndex = sWebPageString.IndexOf("Sell</strong></td><td class=\"align-right\">");
			sWebPageString = sWebPageString.Substring(nIndex + 42);

			// Substring ending at end of sell price line "&nbsp;"
			nIndex = sWebPageString.IndexOf("</td>");
			sWebPageString = sWebPageString.Substring(0, nIndex);

			// Substring starting at first value "<td>
			//nIndex = sWebPageString.IndexOf("<td>");
			//sWebPageString = sWebPageString.Substring(nIndex + 4);

			// Calculate price
			int nGold = 0, nSilver = 0, nCopper = 0;
			int nEndIndex;
			string sSubString;

			// Get gold
			nIndex = sWebPageString.IndexOf("<span class=\"gold\">"); // 19
			if (nIndex != -1)
			{
				nEndIndex = sWebPageString.IndexOf("</span>");
				sSubString = sWebPageString.Substring(nIndex + 19, nEndIndex - (nIndex + 19));
				nGold = Convert.ToInt32(sSubString);
				sWebPageString = sWebPageString.Substring(nEndIndex + 7);
			}

			// Get silver
			nIndex = sWebPageString.IndexOf("<span class=\"silver\">"); // 21
			if (nIndex != -1)
			{
				nEndIndex = sWebPageString.IndexOf("</span>");
				sSubString = sWebPageString.Substring(nIndex + 21, nEndIndex - (nIndex + 21));
				nSilver = Convert.ToInt32(sSubString);
				sWebPageString = sWebPageString.Substring(nEndIndex + 7);
			}

			// Get copper
			nIndex = sWebPageString.IndexOf("<span class=\"copper\">"); // 21
			nEndIndex = sWebPageString.IndexOf("</span>");
			sSubString = sWebPageString.Substring(nIndex + 21, nEndIndex - (nIndex + 21));
			nCopper = Convert.ToInt32(sSubString);
			sWebPageString = sWebPageString.Substring(0, nIndex);

			

			

			

			/*// Get first number
			nIndex = sWebPageString.IndexOf(" <i class");
			string sSubString = sWebPageString.Substring(0, nIndex);
			nNum = Convert.ToInt32(sSubString);
			nIndex = sWebPageString.IndexOf("</i>");
			sSubString = sWebPageString.Substring(nIndex - 1, 1);
			if (sSubString == "g")
			{
				nGold = nNum;
			}
			else if (sSubString == "s")
			{
				nSilver = nNum;
			}
			else if (sSubString == "c")
			{
				nCopper = nNum;
			}

			// Get second number (if applicable)
			sWebPageString = sWebPageString.Substring(nIndex + 4);
			if (sWebPageString.Length == 0)
			{
				goto ComputeTotal;
			}
			nIndex = sWebPageString.IndexOf(" <i class");
			sSubString = sWebPageString.Substring(1, nIndex - 1);
			nNum = Convert.ToInt32(sSubString);
			nIndex = sWebPageString.IndexOf("</i>");
			sSubString = sWebPageString.Substring(nIndex - 1, 1);
			if (sSubString == "s")
			{
				nSilver = nNum;
			}
			else if (sSubString == "c")
			{
				nCopper = nNum;
			}

			// Get third number (if applicable)
			sWebPageString = sWebPageString.Substring(nIndex + 4);
			if (sWebPageString.Length == 0)
			{
				goto ComputeTotal;
			}
			nIndex = sWebPageString.IndexOf(" <i class");
			sSubString = sWebPageString.Substring(1, nIndex);
			nNum = Convert.ToInt32(sSubString);
			nIndex = sWebPageString.IndexOf("</i>");
			sSubString = sWebPageString.Substring(nIndex - 1, 1);
			if (sSubString == "c")
			{
				nCopper = nNum;
			}*/

		ComputeTotal:
			int nTotal = nCopper + (nSilver * 100) + (nGold * 10000);

			return nTotal;
		}


		private string GetWebPageAsString()
		{
			string url = m_sGW2SpidyURL + m_sItemID;

			var sb = new StringBuilder();
			var buf = new byte[8192];

			try
			{

				// prepare the web page we will be asking for
				var request = (HttpWebRequest)
										 WebRequest.Create(url);

				/* Using the proxy class to access the site
				 * Uri proxyURI = new Uri("http://proxy.com:80");
				 request.Proxy = new WebProxy(proxyURI);
				 request.Proxy.Credentials = new NetworkCredential("proxyuser", "proxypassword");*/

				System.Threading.Thread.Sleep(250);

				// execute the request
				var response = (HttpWebResponse)
										   request.GetResponse();

				// we will read data via the response stream
				Stream resStream = response.GetResponseStream();

				string tempString = null;
				int count = 0;

				do
				{
					// fill the buffer with data
					count = resStream.Read(buf, 0, buf.Length);

					// make sure we read some data
					if (count != 0)
					{
						// translate from bytes to ASCII text
						tempString = Encoding.ASCII.GetString(buf, 0, count);

						// continue building the string
						sb.Append(tempString);
					}
				} while (count > 0); // any more data to read?

			}
			catch (Exception exception)
			{
				MessageBox.Show(@"Failed to retrieve data from the network. Please check you internet connection: " +
								exception);
			}

			string result = sb.ToString();
			return result;
		}

		private string GetTestString()
		{
			return "<table class=\"table-condensed table-striped\">\n<tr><td><strong>Sell</strong></td><td class=\"align-right\"><span class=\"silver\">26</span><span class=\"copper\">98</span></td>\n    <td><strong>Supply</strong></td><td class=\"align-right\">349</td></tr>";
		}
	}
}
